#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2021/11/29 11:16
    Desc  :
--------------------------------------
"""
from django_http_log.serializers import DynamicFieldsModelSerializer, DocumentDynamicFieldsModelSerializer
from rest_framework import serializers

from common.utils.log_utils import logger
from common.utils.other_utils import queryDict
from person.models import ProjectPerson
from person.models.person import Person, PersonRegisterHistory, PersonLoginLog


class PersonSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = Person
        fields = '__all__'


class PersonInfoSerializer(DynamicFieldsModelSerializer):
    projectLists = serializers.SerializerMethodField()

    class Meta:
        model = Person
        fields = '__all__'

    def get_projectLists(self, obj):
        if obj:
            if not obj.isSuper:
                return queryDict(
                    sql = f"SELECT T2.id,T2.projectName,T2.projectDesc,"
                          f"T2.projectStatus,T2.remark,T2.createTime,T2.creatorId,"
                          f"T2.creatorName FROM tprojectperson T1 INNER JOIN tproject T2 "
                          f"ON T2.id = T1.projectId AND T2.projectStatus = 102 AND T2.deleted = FALSE "
                          f"WHERE T1.personId = {obj.id} AND T1.deleted = FALSE ORDER BY T2.createTime DESC")
            else:
                return queryDict(
                    sql = f"SELECT T2.id, T2.projectName, T2.projectDesc, T2.projectStatus, "
                          f"T2.remark, T2.createTime, T2.creatorId, T2.creatorName FROM tproject T2 "
                          f"WHERE T2.deleted = FALSE AND T2.projectStatus = 102 ORDER BY T2.createTime DESC")
        return []


class PersonRoleSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = Person
        fields = ['id', 'username', 'realName', 'isActive', 'isSuper', 'phone', 'email', 'createTime']


class PersonListSerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = Person
        fields = ['id', 'username', 'realName', 'isActive', 'isSuper', 'phone', 'email', 'lastLoginTime']


class PersonRegisterHistorySerializer(DynamicFieldsModelSerializer):
    class Meta:
        model = PersonRegisterHistory
        fields = '__all__'


class PersonLoginLogRecordSerializer(DocumentDynamicFieldsModelSerializer):
    class Meta:
        model = PersonLoginLog

        fields = '__all__'
